package model.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;
import dom.domain.User;
import dom.domain.UserDAO;

public class FileService {
	private UserDAO userDao;

	private ToExcel toExcel;
	public void setToExcel(ToExcel toExcel) {
		this.toExcel = toExcel;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}
	

	private Map<String, String> map;
	private String basePath = this.getClass().getClassLoader().getResource("/")
			.getPath().replace("WEB-INF/classes/", "");
	public void setBasePath(String basePath)
	{
		this.basePath = basePath;
	}
	public String ExcelUpload(File file,String FileName,String className,String epId)
	{
		map = new HashMap<String,String>();
		System.out.println(FileName);
			if(FileName==null||!(FileName.endsWith(".xls")||FileName.endsWith(".xlsx")))
			{
				map.put("flag", "false");
				map.put("Info", "文件名为空 or 文件类型错误");
			}
			else if(file==null||!file.exists())
			{
				map.put("flag", "false");
				map.put("Info", "此文件不存在！");
			}else
			{
				if(className.equals("Users"))
				{
					return toExcel.ExcelToUser(file,epId);
				}
				else if(className.equals("Experts"))
				{
					return toExcel.ExcelToExpert(file);
				}
				else if(className.equals("mAdmins"))
				{
					return toExcel.ExcelTomAdmin(file);
				}
				else if(className.equals("Score"))
				{
					return toExcel.ExceltoScore(file,FileName);
				}
			}
			return JSONObject.fromObject(map).toString();
	}
	
	
	public String fileUplad(File file, String FileName, String userName)
			throws IOException {
		map = new HashMap<String, String>();
		if (FileName==null||FileName.endsWith(".exe")) {
			map.put("flag", "flase");
			map.put("message", "不允许上传此类文件！");
		} else if(file==null||!file.exists()) {
			map.put("flag", "false");
			map.put("message", "此文件不存在！");
		} else {
			User tempUser = userDao.findById(userName);
			if (tempUser == null) {
				map.put("flag", "false");
				map.put("message", "用户名不存在！");
			} else {
				FileInputStream fis = new FileInputStream(file);
//				System.out.println(basePath+ "userPhoto/");
				File outFile = new File(basePath+ "userPhoto/");
				if (!outFile.exists()) {
					outFile.mkdirs();
				}
				String path = basePath+ "userPhoto/" + userName + ".jpg";
				FileOutputStream fos = new FileOutputStream(new File(path));
				// System.out.println(outFile.getAbsolutePath());
				byte bt[] = new byte[500];
				while (fis.read(bt) > 0) {
					fos.write(bt);
				}
				fis.close();
				fos.flush();
				fos.close();
				tempUser.setUserPhoto("userPhoto/" + userName + ".jpg");
				userDao.attachDirty(tempUser);
				map.put("flag", "true");
				map.put("message", "上传成功");
			}
		}
		return JSONObject.fromObject(map).toString();
	}
	public InputStream toExcel(String className,String Info,String epId)
	{
		File file = new File(basePath+"Excel/");
		if(!file.exists())
		{
			file.mkdirs();
		}
		if(className.equals("User"))
		{
			Info = "userId#psd#name#gender#eeid#admitId#id#email#phoneNo#userGeneralType#userSubType#userPhoto#userDescribe#birthday#policy#nation#state";
			String path = basePath+"Excel/user_"+epId+".xls";
			return toExcel.UserToExcel(path, Info,Integer.parseInt(epId));
		}
		if(className.equals("mAdmin"))
		{
			try {
				epId=new String(epId.getBytes("ISO8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Info = "adminId#adminPsd#epname";
			String path = basePath+"Excel/mAdmin_"+epId+".xls";
			return toExcel.mAdminToExcel(path, Info,epId);
		}
		if(className.equals("Expert"))
		{
			Info = "expertId#expertName#email#password#epClassId";
			String path = basePath+"Excel/expert_"+epId+".xls";
			return toExcel.ExpertToExcel(path, Info, epId);
		}
		if(className.equals("Classroom"))
		{
			Info = "EPClassId#limitNo";
			String path = basePath+"Excel/ep_"+epId+".xls";
			return toExcel.epToExcel(epId, path);
		}
		return null;
	}
	public InputStream toExcel(String className,String Info)
	{
		
		File file = new File(basePath+"Excel/");
		if(!file.exists())
		{
			file.mkdirs();
		}
		if(className.equals("User"))
		{
			Info = "userId#psd#name#gender#eeid#admitId#id#email#phoneNo#userGeneralType#userSubType#userPhoto#userDescribe#birthday#policy#nation#state";
			String path = basePath+"Excel/user.xls";
			return toExcel.UserToExcel(path, Info);
		}
		if(className.equals("mAdmin"))
		{
			Info = "adminId#adminPsd#epname";
			String path = basePath+"Excel/mAdmin.xls";
			return toExcel.mAdminToExcel(path, Info);
		}
		return null;
	}
	public InputStream fileDownload(String userId) {
		String path = basePath + "userPhoto/"+ userId + ".jpg";
		FileInputStream fileInputStream=null;
		try {
			File outFile = new File(path);
			if (!outFile.exists()) {
				return null;
			}
			fileInputStream= new FileInputStream(
					new File(path));
			return fileInputStream;
		} catch (IOException e) {
			System.out.println(e.getMessage());
			return null;
		}finally
		{
			if(fileInputStream!=null)
			{
				try {
					fileInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}
